<template>
	<tm-fullView>
		<tm-menubars :title="$t('设置')" iconColor="white"></tm-menubars>
		<tm-gap :gutter="30"></tm-gap>
		<view>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('深色模式')" :show-right-icon="false">
				<view slot="rightIcon">
					<tm-switch @change="switchDark" :text="[$t('亮'),$t('暗')]" v-model="dark"></tm-switch>
				</view>
			</tm-listitem>
			<tm-gap :gutter="15"></tm-gap>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('切换语言')" :value="defaultLang!=''?defaultLang:defaultLangName" @click="show=true"></tm-listitem>
			<tm-gap :gutter="15"></tm-gap>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('清除缓存')"></tm-listitem>
			<tm-gap :gutter="15"></tm-gap>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('用户协议')"></tm-listitem>
			<tm-gap :gutter="15"></tm-gap>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('隐私协议')"></tm-listitem>
			<tm-gap :gutter="15"></tm-gap>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('注销用户')"></tm-listitem>
			<tm-gap :gutter="15"></tm-gap>
			<tm-listitem :padding="[40,30]" :margin="[0,0]" :round="0" :title="$t('关于我们')"></tm-listitem>
		</view>
		<view class="logout">
			<tm-button @click="logout" block theme="pink">{{$t('退出登录')}}</tm-button>
		</view>
		<tm-actionSheetMenu :title="$t('请选择语言')"  @change="changeLang" v-model="show" :actions="langs"></tm-actionSheetMenu> 
	</tm-fullView>
</template>

<script>
	import {baseURL,defaultLang} from "@/common/config.js";
	export default {
		data() {
			return {
				dark:false,
				show:false,
				defaultLang:''
			}
		},
		computed:{
			langs(){
				var langList=this.$tm.vx.state().app.config.langList
				let _langs=[]
				langList.forEach(item=>{
					_langs.push(item.name)
				})
				return _langs
			},
			defaultLangName(){
				var name=""
				const lang=uni.getStorageSync("lang")?uni.getStorageSync("lang"):this.$tm.vx.state().translate.defaultLang
				var langList=this.$tm.vx.state().app.config.langList
				langList.forEach(item=>{
					if(item.varName==lang){
						name=item.name
					}
				})
				return name
			}
		},
		onLoad() {
			if(uni.getStorageSync('dark')){
				this.$tm.theme.setBlack(uni.getStorageSync('dark'));
				this.dark=uni.getStorageSync('dark')
			}
			
		},
		methods: {
			switchDark(e){
				this.$tm.theme.setBlack(e.checked);
				uni.setStorageSync("dark",e.checked)
			},
			async changeLang(e){
				let self=this
				self.show=false
				uni.showLoading({
					title:self.$t('正在加载语言')
				})
				var lang=self.$tm.vx.state().app.config.langList[e.index]
				uni.setStorageSync("lang",lang.varName)
				self.$tm.vx.commit('translate/setDefaultLang',lang.varName)
				//初始化翻译
				var res=await self.$tm.vx.actions('translate/getTranslateList')
				uni.setStorageSync('translateList',res)
				setTimeout(()=>{
					uni.hideLoading()
					uni.showToast({
						title:self.$t('语言切换成功')
					})
					self.setTabbar()
					self.defaultLang=lang.name
				},1000)
				
			},
			logout(){
				uni.removeStorageSync("token")
				uni.redirectTo({
					url:'/pages/login/loginbypass'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.logout{
	padding: 50rpx 30rpx;
}
</style>
